<?php
namespace Api\Controller;
use Common\Controller\ApibaseController;

/**
 * 图片文件视频等上传接口
 * @package App\Controller
 */
class FileController extends ApibaseController {
    protected $_id;
    public function _initialize() {
        parent::_initialize();
        $this->checkLogin();
        $this->_id=0;
        $this->image_model = M("File");
    }

    /**
     * APP接口 上传图片,大小限制10240kb
     *
     * @link 访问地址 http://shen_edu.test.yikeapp.cn/index.php?g=app&m=file&a=imageUpload
     * @see string token 登录后获取的凭证
     * @see file file1 上传图片文件
     * @see file file2 上传图片文件
     * @see file file3 上传图片文件
     * @return   [json] 返回图片地址
     *
     * {"status":0,"info":"上传成功","data":[{"id":"65","image":"file\/201706\/5942593a633d5.jpg","url":"http:\/\/localhost\/hua_edu_php\/data\/upload\/file\/201706\/5942593a633d5.jpg"},{"id":"66","image":"file\/201706\/5942593a685de.jpg","url":"http:\/\/localhost\/hua_edu_php\/data\/upload\/file\/201706\/5942593a685de.jpg"}]}
     *
     *              status:0为正常状态，1为请重新登录，>1为数据异常
     *              info:提示信息
     *              data:返回数据
     *                  type: image
     *                  id:图片存储ID
     *                  url：图片链接
     */
    public function imageUpload(){

        $config=array(
            'rootPath'   => './'.C("UPLOADPATH"),
            'savePath'   => './file/',
            'maxSize'    => 10240000,//10240KB
            'saveName'   => array('uniqid',''),
            'exts'       =>  array('jpg', 'png', 'jpeg'),
            'autoSub'    =>  true, //自动子目录保存文件
            'subName'    =>  array('date', 'Ym'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
        );
        $upload = new \Think\Upload($config,'Local');
        $info=$upload->upload();
        //dump($info);exit;
        //开始上传
        if ($info) {
            //上传成功
            //写入附件数据库信息
            $return=true;$pathArray=[];
            foreach ($info as  $value) {
                $file=$value['savepath'].$value['savename'];
                $file=delStr($file,$config['savePath']);
                $image_url=get_bbs_image($file,"file");

                /*生成缩略图*/
                $image_dir=C("UPLOADPATH")."file/";
                $avatar_path=$image_dir.$file;
                $image = new \Think\Image();
                $image->open($avatar_path);
                $image->thumb(1280, $image->height(),1);
                $image->save($avatar_path);/*原图片保存时减小图片质量以缩小图片空间*/
                $image->thumb(640, $image->height(),1);
                $image->save($avatar_path."_small.".$value['ext']);
                unset($image);
                /*生成缩略图*/

                $data=[
                    "user_id"=>$this->_id,
                    "path"=>"file/$file",
                    "create_time"=>date('Y-m-d H:i:s')
                ];
                $imageId=$this->image_model->add($data);
                $return=$return && $imageId;
                $pathArray[]=[
                    "type"=>"image",
                    "id"=>$imageId,
                    "url"=>$image_url,
                    "path"=>$data['path']
                ];
            }
            
            if($return!==false){
                $this->ajaxReturn(1,$pathArray,"上传成功");
            }else{
                $this->ajaxReturn(4,array(),$this->image_model->getError());
            }



        } else {
            //上传失败，返回错误
            $this->ajaxReturn(3,array(),$upload->getError());
        }

    }
}